
const {
    controller_save,
    controller_find,
    controller_get,
    controller_delete,
} = require("../../../controller/product/property/PROPERTY.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");
const pageSizeList = require("../../../utils/slicePageSize");

/**
 * @description 行业类目属性路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/product/property");


/**@description 添加或编辑 */
router.post("/create", async (ctx, next) => {
    try {
        const { id, propertyName, classId,className, propertyValue, status,sort } = ctx.request.body
        const check = emptyCheck({ propertyName, classId,className, propertyValue }, { propertyValue: "Array" })
        if (check.code!== 0) {
            ctx.body = check
        } else {
            const res = await controller_save({
                id, propertyName, classId,className, propertyValue, status,sort
            })
            ctx.body = res
        }
    } catch (error) {
        console.log("错误",error)
        ctx.body = serverError
    }
})

/**@description 根据id获取详情 */
router.get("/get", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await controller_get({ id });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据id删除 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await controller_delete({ ids: [id] });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 查询列表 */
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, propertyName, classId,className, status } = ctx.request.body;
        const res = await controller_find({
            current, pageSize, propertyName, classId,className, status
        })
        ctx.body =res
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})



module.exports = router